home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9063 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  104 lines

  1. Path: nntp.onyx.net!claymoor
  2. From: Adam.Morris@octacon.co.uk (Adam Morris)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: A simple "find the bug" (please! I need help :)
  5. Date: Wed, 28 Feb 96 14:52:28 GMT
  6. Organization: Octacon Ltd
  7. Message-ID: <4h18m5$q4a@mulgave.octacon.co.uk>
  8. References: <4gdr6n$6p0@guava.epix.net>
  9. NNTP-Posting-Host: claymoor.onyx.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4gdr6n$6p0@guava.epix.net>, jgvd@epix.net (Jon) wrote:
  13. >I wrote this just now and it's telling me that when i try to cout the 
  14. >result at the bottom, it is an undefined value even though I did return 
  15. >the result var...  See if you can help.
  16.  
  17. o.k. I've removed the quoted bits from the program so that it looks o.k.
  18.  
  19.  
  20. // --- Program starts here
  21. // include input and output stream stuff
  22. #include <iostream.h>
  23.  
  24. // declare two GLOBAL (these should really be passed in as parameters) 
  25. // variables
  26. float num1;
  27. float num2;
  28.  
  29. // function getnums -- this has no return type, usually that means it will 
  30. // default to int...  we want float so make it so:-
  31.  
  32. float                              // remove this comment, but this is the 
  33. return type
  34. getnums () {
  35.  
  36. // declare a float called result and give it value num1+num2.
  37. // this is a local variable and will vanish when this function ends
  38. float result = num1 +num2;
  39.  
  40. // now we get some numbers in...
  41. cout << "What number?\n";
  42. cin >> num1;
  43. cout << "And?\n";
  44. cin >> num2;
  45.  
  46. // return the value of num1+num2, FROM BEFORE WE GOT THEM IN!!!
  47. return result;
  48. }
  49.  
  50. main (){
  51.  
  52. getnums (); // We are ignoring the return value, we should put it into a 
  53.             // variable
  54.  
  55. cout << result;
  56. }
  57.  
  58. >        ^^^^^^ right there is the result i want printed but it does not 
  59. >recognize it even though I did try to return the value in the function 
  60. >getnums... am I missing something?
  61.  
  62. A few things... try this instead
  63. // --- Program starts here
  64. // include input and output stream stuff
  65. #include <iostream.h>
  66. float
  67. getnums()
  68. {
  69.   // declare two local variables to hold our input numbers
  70.   float theFirstNumber;
  71.   float theSecondNumber;
  72.  
  73.   // now we get some numbers in...
  74.   cout << "What number?" << endl; // let's use C++ stream end lines...
  75.   cin >> theFirstNumber;
  76.   cout << "And?" << endl;
  77.   cin >> theSecondNumber;
  78.  
  79.   // now we create another local variable to hold the result
  80.   float theResult = theFirstNumber + theSecondNumber;
  81.   //and send it back to the calling function
  82.   return theResult;
  83.   // not this last bit is inefficient, we could have just used 
  84.   // return(theFirstNumber + theSecondNumber);
  85. }
  86.  
  87. main ()
  88. {
  89.   float aResult= getnums(); // create a local variable to hold the result of 
  90.                             // our call to getnums Notice that this doesn't
  91.                             // need to have the same name as the returned
  92.                             // variable in the getnums() function
  93.  
  94.   cout << aResult;  // output it.
  95. }
  96.  
  97. Adam
  98.  
  99. P.S.  any typos in this are all my fault, I haven't compiled it, and I typed 
  100. it into my newsreader...
  101.  
  102.  
  103.  
  104.